const app = getApp()
const $ = app.globalData
Page({
	data: {
		tabs: ["全部", "待取货", "待发货", "待收货", "已完成"],
		activeIndex: 0,
		page: 1,
		orderstate: "",
		orderList: [{
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}, {
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}, {
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}, {
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}, {
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}]
	},
	onLoad: function(options) {
		var that = this
		wx.getStorage({
			key: "userinfo",
			success: function(res) {
				that.setData({
					userinfo: res.data
				})
				that.getorderlist()
			}
		})
		wx.stopPullDownRefresh();
	},
	onShow: function() {
		var that = this
		if ($.onshow) {
			that.setData({
				orderList: [{
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}]
			})
			that.onLoad();
		}
	},
	onHide: function() {

	},
	onUnload: function() {
		$.onshow = false
	},
	// 获取订单
	getorderlist: function() {
		var that = this
		var expresstype = ''
		var activeIndexxx = ''
		var orderList = that.data.orderList
		switch (that.data.activeIndex) {
			case 0:
				activeIndexxx = '';
				break;
			case 1:
				activeIndexxx = 2;
				expresstype = 1;
				break;
			case 2:
				activeIndexxx = 2;
				expresstype = 0;
				break;
			case 3:
				activeIndexxx = 3;

				break;
			case 4:
				activeIndexxx = 6;
				break;
		}
		if (orderList[that.data.activeIndex].falis) {
			wx.request({
				url: $.AdminUrl + "/YApi/getShopOrder",
				data: {
					shopid: $.myshopid,
					name: "",
					orderstate: activeIndexxx,
					expresstype: expresstype,
					page: that.data.orderList[that.data.activeIndex].page
				},
				success: (res) => {
					wx.stopPullDownRefresh();
					var order = res.data.obj
					orderList[that.data.activeIndex].list = orderList[that.data.activeIndex].list.concat(order)
					orderList[that.data.activeIndex].falis = false
					if (order.length >= 10) {
						orderList[that.data.activeIndex].gengduo = true
					} else {
						orderList[that.data.activeIndex].gengduo = false
					}
					console.log(orderList)
					that.setData({
						orderList: orderList
					})
				}
			})
		}
	},
	// 加载更多
	loadgengduo: function() {
		var that = this
		var orderList = that.data.orderList
		var page = orderList[that.data.activeIndex].page + 1
		orderList[that.data.activeIndex].page = page
		orderList[that.data.activeIndex].falis = true
		that.setData({
			page: page,
			orderList: orderList
		})
		that.getorderlist()
	},
	// 查看订单详情
	lookxq: function(e) {
		var thisid = e.currentTarget.dataset.id
		wx.navigateTo({
			url: 'orderxq/orderxq?id=' + thisid
		})
	},
	// 去发货
	gofahuo: function(e) {
		var thisid = e.currentTarget.dataset.id
		wx.navigateTo({
			url: 'orderxq/orderxq?id=' + thisid
		})
		return false;
	},
	// 去发货
	goquhuo: function(e) {
		var thisid = e.currentTarget.dataset.id
		wx.navigateTo({
			url: 'orderxq/orderxq?id=' + thisid + "&quhuo=1"
		})
	},
	bindChange: function(e) {
		var that = this
		var activeIndex = parseInt(e.currentTarget.id)
		var orderlist = that.data.orderList
		var page = orderlist[e.currentTarget.id].page
		that.setData({
			activeIndex: activeIndex,
			page: page
		});
		that.getorderlist()
	},
	// 去订单页面
	gomy: function() {
		wx.redirectTo({
			url: "../my/my"
		})
	},
	// 去我的页面
	goindex: function() {
		wx.redirectTo({
			url: "../index/index"
		})
	},
	// 上拉刷新
	onPullDownRefresh() {
		var that = this
			that.setData({
				orderList: [{
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}]
			})
			that.onLoad();
	  },
});
